package mydictionaryry;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.BrokenBarrierException;

/**
 * @author Administrator
 * 工具实现类:文件实现
 */
public class DictionaryDaoIO implements DictionaryDao {
  @Override
  public void save(MyDictionary myDictionary) {
    BufferedWriter bufferedWriter=null;
    try {
      // 追加内容
      bufferedWriter=new BufferedWriter(new FileWriter("d:\\d.txt",true));
      bufferedWriter.write(myDictionary.getId()+"--"+myDictionary.getEnglish()+"--"
          +myDictionary.getChinese()+"--"+myDictionary.getComments());
      bufferedWriter.newLine();
      bufferedWriter.flush();
      System.out.println("保存成功");
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      if(bufferedWriter!=null){
        try {
          bufferedWriter.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  @Override
  public MyDictionary query(String name) {
    BufferedReader bufferedReader=null;
    try {
      bufferedReader=new BufferedReader(new FileReader("d:\\d.txt"));
      String line;
      while((line=bufferedReader.readLine())!=null){
        String[] data=line.split("--");
        if(name.equals(data[1])){
          int id=Integer.parseInt(data[0]);
          String english=data[1];
          String chinese=data[2];
          String comments=data[3];
          MyDictionary d=new MyDictionary(id,english,chinese,comments);
          return d;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      if(bufferedReader!=null){
        try {
          bufferedReader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return null;
  }

  @Override
  public void queryAll() {
    BufferedReader bufferedReader=null;
    try {
      bufferedReader=new BufferedReader(new FileReader("D:\\d.txt"));
      String line=null;
      while((line=bufferedReader.readLine())!=null){
        System.out.println(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      if(bufferedReader!=null){
        try {
          bufferedReader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  @Override
  public void delete(String name) {
    if(query(name)==null){
      System.out.println("要删除的内容不存在");
      return;
    }else{
      BufferedWriter bufferedWriter = null;
      BufferedReader bufferedReader=null;
      try {
        bufferedReader=new BufferedReader(new FileReader("d:\\d.txt"));
        bufferedWriter=new BufferedWriter(new FileWriter("d:\\new.txt",true));
        String line;
        while ((line=bufferedReader.readLine())!=null){
          String[] data=line.split("--");
          if(!name.equals(data[1])){
            bufferedWriter.write(line);
            bufferedWriter.newLine();
            bufferedWriter.flush();
          }
        }

      } catch (Exception e) {
        e.printStackTrace();
      }finally {
        if(bufferedReader!=null){
          try {
            bufferedReader.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        if(bufferedWriter!=null){
          try {
            bufferedWriter.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    File oldFile=new File("d:\\d.txt");
    File newFile =new File("d:\\new.txt");

    oldFile.delete();
    newFile.renameTo(oldFile);
    System.out.println("删除成功");
  }
}
